<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Sign In
 * @description : User sign in mobile
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Signin extends MOBILE_Controller {

  public function index() {

    // Checking request type
    if ($this->is_post()) {

      // Getting inputs
      $username = $this->input->post('username', TRUE);
      $password = $this->input->post('password', TRUE);

      // Checking inputs
      if (!empty($username) && !empty($password)) {

        // Doing signin
        $session_id = $this->user_session_mobile->signin($username, md5($password));

        // Checking sigin
        if (!empty($session_id)) {

          // Setting current user
          $this->mobile_current_user = $this->user_session_mobile->signuser($session_id);

          $this->response['status'] = 1;
          $this->response['message'] = 'Success';
          $this->response['data'] = array('user' => $this->make_response($this->mobile_current_user), 'session_id' => $session_id);
        } else {
          $this->response['message'] = 'Sign in failed';
        }
      } else {
        $this->response['message'] = 'Required username / email and password';
      }
    } else {
      $this->response['message'] = 'Invalid request';
    }
  }

  private function make_response($user_data = array()) {
    $user_details['user_id'] = $user_data['user_id'];
    $user_details['user_name'] = $user_data['user_name'];
    $user_details['user_display_name'] = $user_data['user_display_name'];
    $user_details['user_likes_level'] = $user_data['user_likes_level'];
    $user_details['user_gender'] = $user_data['user_gender'];
    $user_details['user_position'] = $user_data['user_position'];
    $user_details['user_skills'] = $user_data['user_skills'];
    $user_details['user_hobbies'] = $user_data['user_hobbies'];
    $user_details['user_place'] = $user_data['user_place'];
    $user_details['user_profile_url'] = site_url('profile/' . $user_data['user_name']);
    $user_details['user_image_url'] = site_url('assets/photos/' . $user_data['user_image']);
    return $user_details;
  }

  public function __destruct() {
    echo json_encode($this->response);
  }

}

?>